package com.ruoyi.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.account.enums.ActionTypeEnum;
import com.ruoyi.account.enums.ApplyStatusEnum;
import com.ruoyi.account.service.IAccountApplyRecordService;
import com.ruoyi.account.service.IAccountInfoService;
import com.ruoyi.account.service.IAccountMerchantInfoService;
import com.ruoyi.account.service.IAccountProductInfoService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.AccountApplyRecord;
import com.ruoyi.laiqianba.enums.AccountTypeEnum;
import com.ruoyi.laiqianba.service.IAdminHttpService;
import com.ruoyi.web.service.IApplyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ApplyRecordService implements IApplyRecordService {

    @Autowired
    IAccountMerchantInfoService accountMerchantInfoService;
    @Autowired
    IAccountProductInfoService accountProductInfoService;
    @Autowired
    IAccountInfoService accountInfoService;
    @Autowired
    IAccountApplyRecordService accountApplyRecordService;
    @Autowired
    IAdminHttpService adminHttpService;

    @Override
    public List<Map<String, String>> getItemAccountInfoMapList(String accountType){

        List<Map<String, String>> resultMapList = new ArrayList<>();

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = null;
        if (!SecurityUtils.loginUserIsAdmin()){
            userId = loginUser.getUserId();
        }

        if (StrUtil.equals(accountType, AccountTypeEnum.ACCOUNT.getValue())){
            resultMapList = accountInfoService.getSimpleMapListByOwnerId();
        }else if (StrUtil.equals(accountType, AccountTypeEnum.MERCHANT.getValue())){
            resultMapList = accountMerchantInfoService.getSimpleMapListByOwnerId(userId);
        }else if (StrUtil.equals(accountType, AccountTypeEnum.PRODUCT.getValue())){
            resultMapList = accountProductInfoService.getSimpleMapListByOwnerId(userId);
        }
        return resultMapList;
    }

    @Override
    public AjaxResult applyBill(Long billId, Integer type){

        AccountApplyRecord accountApplyRecord = new AccountApplyRecord();
        accountApplyRecord.setId(billId);

        if (type == 1){
            accountApplyRecord.setStatus(ApplyStatusEnum.WAIT.getValue());
        }else{
            accountApplyRecord.setStatus(ApplyStatusEnum.INIT.getValue());
        }
        return accountApplyRecordService.updateAccountApplyRecord(accountApplyRecord)>0?AjaxResult.success():AjaxResult.error();
    }

    @Override
    public AjaxResult enterBill(Long billId, SysUser sysUser){

        AccountApplyRecord accountApplyRecord = accountApplyRecordService.selectAccountApplyRecordById(billId);
        // TODO: 2023/10/18 数据信息判断

        // 充值和退款必须选择总账信息
        if (StrUtil.equalsAny(accountApplyRecord.getActionType(),
                ActionTypeEnum.CZ.getValue(),
                ActionTypeEnum.TZ.getValue(),
                ActionTypeEnum.TK.getValue())){
            //
            if (ObjectUtil.isEmpty(accountApplyRecord.getMainAccountId())){
                return AjaxResult.error("充值退款操作需要选择总账信息");
            }
        }

        //操作金额都必须为正数
        if (BigDecimal.ZERO.compareTo(accountApplyRecord.getBillAmount()) >= 0){
            return AjaxResult.error("账单金额必须都为正数");
        }

        if (!ObjectUtil.equals(accountApplyRecord.getStatus(), ApplyStatusEnum.WAIT.getValue())){
            return AjaxResult.error("账单状态错误，不可操作");
        }

        if (ObjectUtil.isEmpty(accountApplyRecord.getImgUrl())){
            return AjaxResult.error("账单图片不能为空");
        }

        return adminHttpService.enterAccount(String.valueOf(billId), String.valueOf(sysUser.getUserId()), sysUser.getNickName());
    }
}
